﻿using BZOrderPrint.DAL;
using BZOrderPrint.DAL.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BZOrderPrint.BLL.Helper;
using BZOrderPrint.Model;
using System.Diagnostics;
using System.IO;
using BZOrderPrint.PDF.Model;
using BZOrderPrint.PDF;
using static BZOrderPrint.DAL.AppEnum;
using System.Threading;

namespace BZOrderPrint
{
    public partial class WhloeOrderForm : Form
    {
        private BZContext context;

        private List<BzShopEntity> shopList;

        public WhloeOrderForm()
        {
            InitializeComponent();
            Init();
            DataGirdViewInit(GetWhloeOrder(1));
        }

        private void Init()
        {
            context = new BZContext(ConfigHelper.ConnString);
            shopList = context.bzShopEntities.ToList();
        }

        private void WhloeOrderForm_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 1;
            if (!int.TryParse(pageIndex.Text, out i))
                MessageBox.Show("页码错误");

            DataGirdViewInit(GetWhloeOrder(i));
        }

        private void DataGirdViewInit(PageWholeOrderModel model)
        {
            var data = new DataTable();
            dataGridView1.Columns.Clear();
            var dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "ID",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "批发开单号",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "银豹批发开单号",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "网点名称",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "创建日期",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "OrderShopId",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);
            dc = new DataColumn()
            {
                DataType = Type.GetType("System.String"),
                ColumnName = "IsGroup",
                DefaultValue = "",
                ReadOnly = true
            };
            data.Columns.Add(dc);

            if (model.bzWhloeOrderEntities.Count > 0)
            {
                foreach (var item in model.bzWhloeOrderEntities)
                {
                    var dataRow = data.NewRow();
                    dataRow["ID"] = item.Id;
                    dataRow["批发开单号"] = item.WhloeOrderCode;
                    dataRow["银豹批发开单号"] = item.YbOrderCode;
                    dataRow["网点名称"] = shopList.FirstOrDefault(x => x.Id == item.BzShopeId)?.ShopName ?? "";
                    dataRow["创建日期"] = item.CreateTime.ToString("yyyy-MM-dd hh:mm:ss");
                    dataRow["OrderShopId"] = item.BzShopeId;
                    dataRow["IsGroup"] = item.IsGroup;
                    data.Rows.Add(dataRow);
                }
            }

            DataGridViewCheckBoxColumn checkColum = new DataGridViewCheckBoxColumn
            {
                HeaderText = "选择",
                Name = "IsChecked",
                DataPropertyName = "IsChecked",
                ReadOnly = false,
                TrueValue = true,
                FalseValue = false,
                Width = 40
            };
            dataGridView1.Columns.Insert(0, checkColum);
            DataGridViewButtonColumn buttonColum = new DataGridViewButtonColumn
            {
                Text = "查看",
                Name = "查看",
                ToolTipText = "查看",
                ReadOnly = false,
                Width = 40
            };
            dataGridView1.Columns.Insert(1, buttonColum);
            dataGridView1.DataSource = data;
            dataGridView1.Columns[1].Width = 60;
            dataGridView1.Columns[2].Width = 60;
            dataGridView1.Columns[3].Width = 180;
            dataGridView1.Columns[4].Width = 180;
            dataGridView1.Columns[5].Width = 180;
            dataGridView1.Columns[6].Width = 230;
            //dataGridView1.Columns[6].Visible = false;
            dataGridView1.Columns[7].Visible = false;
            dataGridView1.Columns[8].Visible = false;
            dataGridView1.ReadOnly = false;
            for (int i = 0; i <= dataGridView1.Columns.Count - 1; i++)
            {
                dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }
        }

        private PageWholeOrderModel GetWhloeOrder(int PageIndex)
        {
            PageWholeOrderModel model = new PageWholeOrderModel();
            Expression<Func<BzWhloeOrderEntity, bool>> predicate = x => true;
            if (!string.IsNullOrEmpty(whole_order.Text))
            {
                predicate = predicate.And(x => x.WhloeOrderCode.Contains(whole_order.Text));
            }
            if (!string.IsNullOrEmpty(yb_order.Text))
            {
                predicate = predicate.And(x => x.YbOrderCode.Contains(whole_order.Text));
            }

            var dataList = context.bzWhloeOrderEntities.Where(predicate).OrderByDescending(x => x.CreateTime);
            if (dataList.Count() > 0)
            {
                model.TotalRows = dataList.Count();
                model.TotalPage = dataList.Count() / 30 + 1;
                model.PageIndex = PageIndex;
                model.PageSize = 30;
                model.bzWhloeOrderEntities = dataList.Skip((PageIndex - 1) * 30).Take(30).ToList();
            }
            else
            {
                model.TotalRows = 0;
                model.TotalPage = 0;
                model.PageIndex = PageIndex;
                model.PageSize = 30;
            }
            return model;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var orders = GetCheckItem();
            if (orders.Count > 1)
            {
                MessageBox.Show("一次只能导出一条数据");
                return;
            }
            if (orders.Count <= 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }
            if (string.IsNullOrEmpty(orders.FirstOrDefault().YbOrderCode))
            {
                MessageBox.Show("银豹批发开单号为空不能导出");
                return;
            }
            //if (!PrintHelper.Verification())
            //{
            //    MessageBox.Show("请设置打印机");
            //    return;
            //}
            //PrintHelper.PaperPrinter();
            if (orders.FirstOrDefault().IsGroup == (int)GroupType.ShopOrder)
            {
                string whloeOrderCode = orders.FirstOrDefault().WhloeOrderCode;
                int shopId = orders.FirstOrDefault().BzShopeId;
                var lines = context.bzYbLineEntities.Where(x => x.BzWhloeOrderCode == whloeOrderCode).ToList();
                ShopPackageOrder packageOrder = new ShopPackageOrder();
                var orderIds = lines.Select(o => o.BzOrderId).ToList();

                var shop = context.bzShopEntities.FirstOrDefault(x => x.Id == shopId);
                packageOrder.ShopAdmin = shop.ShopAdmin;
                packageOrder.ShopName = shop.ShopName;
                packageOrder.YbOrderCode = orders.FirstOrDefault().YbOrderCode;
                packageOrder.Phone = shop.Phone;

                List<BzOrderItemEntity> orderItems = new List<BzOrderItemEntity>();
                var items = context.bzOrderItemEntities.Where(x => orderIds.Contains(x.OrderId)).ToList();
                var itemCodes = items.Select(x => x.OrderItemCode).Distinct();
                foreach (var info in itemCodes)
                {
                    var count = items.Where(x => x.OrderItemCode == info).Sum(i => i.Quantity);
                    var item = items.FirstOrDefault(x => x.OrderItemCode == info);
                    BzOrderItemEntity itemEntity = new BzOrderItemEntity();
                    itemEntity.Id = item.Id;
                    itemEntity.OrderId = item.OrderId;
                    itemEntity.CreateTime = item.CreateTime;
                    itemEntity.OrderItem = item.OrderItem;
                    itemEntity.OrderItemCode = item.OrderItemCode;
                    itemEntity.Price = item.Price;
                    itemEntity.Quantity = count;
                    orderItems.Add(itemEntity);
                }
                packageOrder.orderItems = orderItems;
                string path = PDFHelper.CreateShopOrderPackagePDF(packageOrder);
                //PDFHelper.PrintPDF(path);
                MessageBox.Show("导出完成");
                Process.Start("Explorer", "/select," + path);
            }
            else
            {
                string whloeOrderCode = orders.FirstOrDefault().WhloeOrderCode;
                int shopId = orders.FirstOrDefault().BzShopeId;
                var lines = context.bzYbLineEntities.Where(x => x.BzWhloeOrderCode == whloeOrderCode).ToList();
                int bzOrderId = lines.FirstOrDefault().BzOrderId;
                var bzorder = context.bzOrderEntities.FirstOrDefault(x => x.Id == bzOrderId);
                TeamPackageOrder teamPackageOrder = new TeamPackageOrder();
                teamPackageOrder.AdminAddress = bzorder.Address;
                teamPackageOrder.AdminName = bzorder.MemberName;
                teamPackageOrder.Phone = bzorder.Phone;
                teamPackageOrder.YbOrderCode = orders.FirstOrDefault().YbOrderCode;
                teamPackageOrder.ShopName = bzorder.OrderShopName;
                var items = context.bzOrderItemEntities.Where(x => x.OrderId == bzOrderId);
                if (items.Count() == 1)
                {
                    teamPackageOrder.orderItems.Add(items.FirstOrDefault());
                }
                else
                {
                    var itemCodes = items.Select(x => x.OrderItemCode).Distinct();
                    foreach (var info in itemCodes)
                    {
                        var count = items.Where(x => x.OrderItemCode == info).Sum(i => i.Quantity);
                        var item = items.FirstOrDefault(x => x.OrderItemCode == info);
                        BzOrderItemEntity itemEntity = new BzOrderItemEntity();
                        itemEntity.Id = item.Id;
                        itemEntity.OrderId = item.OrderId;
                        itemEntity.CreateTime = item.CreateTime;
                        itemEntity.OrderItem = item.OrderItem;
                        itemEntity.OrderItemCode = item.OrderItemCode;
                        itemEntity.Price = item.Price;
                        itemEntity.Quantity = count;
                        teamPackageOrder.orderItems.Add(itemEntity);
                    }
                }
                string path = PDFHelper.CreateTeamOrderPackagePDF(teamPackageOrder);
                //PDFHelper.PrintPDF(path);
                MessageBox.Show("导出完成");
                Process.Start("Explorer", "/select," + path);
            }
        }

        private List<BzWhloeOrderEntity> GetCheckItem()
        {
            List<BzWhloeOrderEntity> orders = new List<BzWhloeOrderEntity>();
            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    orders.Add(GetWhloeOrder(dataGridView1.Rows[i]));
                }
            }
            return orders;
        }

        private BzWhloeOrderEntity GetWhloeOrder(DataGridViewRow data)
        {
            BzWhloeOrderEntity entity = new BzWhloeOrderEntity();

            entity.Id = int.Parse(data.Cells["ID"].Value.ToString());
            entity.WhloeOrderCode = data.Cells["批发开单号"].Value?.ToString();
            entity.YbOrderCode = data.Cells["银豹批发开单号"].Value?.ToString();
            entity.BzShopeId = int.Parse(data.Cells["OrderShopId"].Value.ToString());
            entity.IsGroup = int.Parse(data.Cells["IsGroup"].Value.ToString());
            entity.CreateTime = DateTime.Parse(data.Cells["创建日期"].Value.ToString());
            return entity;
        }

        private void ExportExcel(List<BzOrderItemEntity> orderItems)
        {
            for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
            {
                if ((bool)dataGridView1.Rows[i].Cells[0].EditedFormattedValue == true)
                {
                    int Id = int.Parse(dataGridView1.Rows[i].Cells["ID"].Value.ToString());
                    var items = context.bzOrderItemEntities.Where(x => x.OrderId.Equals(Id)).ToList();
                    if (items.Any())
                    {
                        orderItems.AddRange(items);
                    }
                    //OrderDetailModel detailModel = new OrderDetailModel();
                    //detailModel.order = GetOrder(dataGridView1.Rows[i]);
                    //detailModel.orderItems = context.bzOrderItemEntities.Where(x => x.OrderId == detailModel.order.Id).ToList();
                    //detailModels.Add(detailModel);
                }
            }

            if (orderItems.Count > 0)
            {
                var data = new DataTable();
                var dc = new DataColumn()
                {
                    DataType = Type.GetType("System.String"),
                    ColumnName = "商品名称",
                    DefaultValue = "",
                    ReadOnly = true
                };
                data.Columns.Add(dc);
                dc = new DataColumn()
                {
                    DataType = Type.GetType("System.String"),
                    ColumnName = "条码（必填）",
                    DefaultValue = "",
                    ReadOnly = true
                };
                data.Columns.Add(dc);
                dc = new DataColumn()
                {
                    DataType = Type.GetType("System.String"),
                    ColumnName = "售价",
                    DefaultValue = "",
                    ReadOnly = true
                };
                data.Columns.Add(dc);
                dc = new DataColumn()
                {
                    DataType = Type.GetType("System.String"),
                    ColumnName = "数量",
                    DefaultValue = "",
                    ReadOnly = true
                };
                data.Columns.Add(dc);
                dc = new DataColumn()
                {
                    DataType = Type.GetType("System.String"),
                    ColumnName = "赠送量",
                    DefaultValue = "",
                    ReadOnly = true
                };
                data.Columns.Add(dc);
                var orderItemCodes = orderItems.Select(x => x.OrderItemCode).Distinct().ToList();
                foreach (var orderItemCode in orderItemCodes)
                {
                    var orderItem = orderItems.FirstOrDefault(x => x.OrderItemCode.Equals(orderItemCode));
                    var dataRow = data.NewRow();
                    dataRow["商品名称"] = orderItem?.OrderItem?.Split('（').FirstOrDefault();
                    dataRow["条码（必填）"] = orderItem?.OrderItemCode;
                    dataRow["售价"] = "";
                    dataRow["数量"] = orderItems.Where(x => x.OrderItemCode.Equals(orderItemCode)).Sum(x => x.Quantity);
                    dataRow["赠送量"] = "";
                    data.Rows.Add(dataRow);
                }

                var fileName = $"批发单开单-{DateTime.Now:yyyyMMddHHmm}.xlsx";
                var directory = Environment.CurrentDirectory + @"\tempfile";
                if (!Directory.Exists(directory))//如果不存
                {
                    Directory.CreateDirectory(directory);
                }
                ExcelHelper.DataTableToExcel(data, directory + @"\" + fileName, true);
                Process.Start("Explorer", "/select," + directory + @"\" + fileName);
                MessageBox.Show("导出完成");
            }
            else
                MessageBox.Show("请选择需要导出的数据");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var orders = GetCheckItem();
            if (orders.Count > 1)
            {
                MessageBox.Show("一次只能导出一条数据");
                return;
            }
            if (orders.Count <= 0)
            {

                MessageBox.Show("请选择要导出的数据");
                return;
            }
            List<BzOrderItemEntity> orderItems = new List<BzOrderItemEntity>();

            string whloeOrderCode = orders.FirstOrDefault().WhloeOrderCode;
            var lines = context.bzYbLineEntities.Where(x => x.BzWhloeOrderCode == whloeOrderCode).ToList();

            var orderIds = lines.Select(o => o.BzOrderId).ToList();
            //var bzorders = context.bzOrderEntities.Where(x => orderIds.Contains(x.Id)).ToList();
            var items = context.bzOrderItemEntities.Where(x => orderIds.Contains(x.OrderId)).ToList();

            var itemCodes = items.Select(x => x.OrderItemCode).Distinct();
            foreach (var info in itemCodes)
            {
                //int count = items.Count(x => x.OrderItemCode == info);
                //var item = items.FirstOrDefault(x => x.OrderItemCode == info);

                var count = items.Where(x => x.OrderItemCode == info).Sum(i => i.Quantity);
                var item = items.FirstOrDefault(x => x.OrderItemCode == info);
                BzOrderItemEntity itemEntity = new BzOrderItemEntity();
                itemEntity.Id = item.Id;
                itemEntity.OrderId = item.OrderId;
                itemEntity.CreateTime = item.CreateTime;
                itemEntity.OrderItem = item.OrderItem;
                itemEntity.OrderItemCode = item.OrderItemCode;
                itemEntity.Price = item.Price;
                itemEntity.Quantity = count;
                orderItems.Add(itemEntity);
            }
            ExportExcel(orderItems);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var orders = GetCheckItem();
            if (orders.Count > 1)
            {
                MessageBox.Show("一次只能导出一条数据");
                return;
            }
            if (orders.Count <= 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }
            if (string.IsNullOrEmpty(orders.FirstOrDefault().YbOrderCode))
            {
                MessageBox.Show("银豹批发开单号为空不能导出");
                return;
            }
            //if (!PrintHelper.Verification())
            //{
            //    MessageBox.Show("请设置打印机");
            //    return;
            //}
            PrintHelper.PaperPrinter();
            if (orders.FirstOrDefault().IsGroup == (int)GroupType.ShopOrder)
            {
                string whloeOrderCode = orders.FirstOrDefault().WhloeOrderCode;
                var lines = context.bzYbLineEntities.Where(x => x.BzWhloeOrderCode == whloeOrderCode).ToList();
                var orderIds = lines.Select(o => o.BzOrderId).ToList();
                var bzorders = context.bzOrderEntities.Where(x => orderIds.Contains(x.Id)).ToList();
                List<ShopPickUpOrder> pickUpOrders = new List<ShopPickUpOrder>();
                string shopName = bzorders.FirstOrDefault().OrderShopName;
                foreach (var item in bzorders)
                {
                    ShopPickUpOrder pickUpOrder = new ShopPickUpOrder();
                    pickUpOrder.ShopName = shopName;
                    pickUpOrder.WhloeOrderCorde = whloeOrderCode;
                    pickUpOrder.order = item;
                    pickUpOrder.orderItems = context.bzOrderItemEntities.Where(x => x.OrderId == item.Id).ToList();
                    pickUpOrders.Add(pickUpOrder);
                }
                string path = PDFHelper.CreateShopOrderPickUpPDF(pickUpOrders);
                //PDFHelper.PrintPDF(path);
                MessageBox.Show("导出完成");
                Process.Start("Explorer", "/select," + path);
            }
            else
            {
                string whloeOrderCode = orders.FirstOrDefault().WhloeOrderCode;
                var lines = context.bzYbLineEntities.Where(x => x.BzWhloeOrderCode == whloeOrderCode).ToList();
                var orderIds = lines.Select(o => o.BzOrderId).ToList();
                var bzorders = context.bzOrderEntities.Where(x => orderIds.Contains(x.Id)).ToList();
                if (bzorders.Count == 1)
                {
                    string bzOrderCode = bzorders.FirstOrDefault().OrderCode;
                    TeamPickUpOrder teamPickUpOrder = new TeamPickUpOrder();
                    teamPickUpOrder.WhloeOrderCorde = whloeOrderCode;
                    teamPickUpOrder.ShopName = bzorders.FirstOrDefault().OrderShopName;
                    teamPickUpOrder.Address = bzorders.FirstOrDefault().Address;
                    teamPickUpOrder.AdminName = bzorders.FirstOrDefault().MemberName;
                    teamPickUpOrder.Phone = bzorders.FirstOrDefault().Phone;
                    teamPickUpOrder.itemEntities = context.teamOrderItemEntities.Where(x => x.BzOrderId == bzOrderCode).ToList();
                    string path = PDFHelper.CreateTeamOrderPickUpPDF(teamPickUpOrder);
                    //PDFHelper.PrintPDF(path);
                    MessageBox.Show("导出完成");
                    Process.Start("Explorer", "/select," + path);
                }

            }
        }
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == dataGridView1.Columns[1].Index)
            {
                var Id = int.Parse(dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString());
                var item = context.bzWhloeOrderEntities.FirstOrDefault(x => x.Id.Equals(Id));
                FrmWhloeOrder frmWhloeOrder = new FrmWhloeOrder(item);
                var result = frmWhloeOrder.ShowDialog();
                if (result == DialogResult.OK)
                {
                    DataGirdViewInit(GetWhloeOrder(1));
                }

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int i = 1;
            if (!int.TryParse(pageIndex.Text, out i))
                MessageBox.Show("页码错误");

            DataGirdViewInit(GetWhloeOrder(++i));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i = 1;
            if (!int.TryParse(pageIndex.Text, out i))
                MessageBox.Show("页码错误");

            DataGirdViewInit(GetWhloeOrder(--i));
        }

        private void button7_Click(object sender, EventArgs e)
        {
            var orders = GetCheckItem();
            if (orders.Count > 1)
            {
                MessageBox.Show("一次只能导出一条数据");
                return;
            }
            if (orders.Count <= 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }
            if (string.IsNullOrEmpty(orders.FirstOrDefault().YbOrderCode))
            {
                MessageBox.Show("银豹批发开单号为空不能导出");
                return;
            }
            if (!PrintHelper.Verification())
            {
                MessageBox.Show("请设置打印机");
                return;
            }
            PrintHelper.FaceSheetPrinter();
            string whloeOrderCode = orders.FirstOrDefault().WhloeOrderCode;
            var lines = context.bzYbLineEntities.Where(x => x.BzWhloeOrderCode == whloeOrderCode).ToList();
            var orderIds = lines.Select(o => o.BzOrderId).ToList();
            var bzorders = context.bzOrderEntities.Where(x => orderIds.Contains(x.Id)).ToList();
            foreach (var item in bzorders)
            {
                if (item.IsGroup == 0)
                    LabelHelper.PrintShopLabel(item);
                else
                    LabelHelper.PrintTeamLabel(item);
            }
            MessageBox.Show("打印完成");

        }

        private void button8_Click(object sender, EventArgs e)
        {
            var orders = GetCheckItem();
            if (orders.Count <= 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }
            if (!PrintHelper.Verification())
            {
                MessageBox.Show("请设置打印机");
                return;
            }
            PrintHelper.PaperPrinter();
            foreach (var item in orders)
            {
                if (string.IsNullOrEmpty(item.YbOrderCode))
                {
                    MessageBox.Show(item.WhloeOrderCode + "银豹批发开单号为空不能打印");
                    return;
                }
                string pdfPath = AppDomain.CurrentDomain.BaseDirectory + @"PDFFile\Package\";
                string pdfFullPath = pdfPath + item.YbOrderCode + ".pdf";
                if (!File.Exists(pdfFullPath))
                {
                    MessageBox.Show(item.WhloeOrderCode + "文件不存在，请先导出");
                    return;
                }
                PDFHelper.PrintPDF(pdfFullPath);
            }
            MessageBox.Show("打印完成");
        }

        private void button9_Click(object sender, EventArgs e)
        {
            var orders = GetCheckItem();
            if (orders.Count <= 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }
            if (!PrintHelper.Verification())
            {
                MessageBox.Show("请设置打印机");
                return;
            }
            PrintHelper.PaperPrinter();

            foreach (var item in orders)
            {
                if (string.IsNullOrEmpty(item.YbOrderCode))
                {
                    MessageBox.Show(item.WhloeOrderCode + "银豹批发开单号为空不能打印");
                    return;
                }
                string pdfPath = AppDomain.CurrentDomain.BaseDirectory + @"PDFFile\PickUp\";
                string pdfFullPath = pdfPath + item.WhloeOrderCode + ".pdf";
                if (!File.Exists(pdfFullPath))
                {
                    MessageBox.Show("文件不存在，请先导出");
                    return;
                }
                PDFHelper.PrintPDF(pdfFullPath);

            }
            MessageBox.Show("打印完成");
        }

        private void button10_Click(object sender, EventArgs e)
        {
            var orders = GetCheckItem();
            if (orders.Count > 1)
            {
                MessageBox.Show("一次只能导出一条数据");
                return;
            }
            if (!PrintHelper.Verification())
            {
                MessageBox.Show("请设置打印机");
                return;
            }
            if (orders.Count <= 0)
            {
                MessageBox.Show("请选择数据");
                return;
            }
            if (string.IsNullOrEmpty(orders.FirstOrDefault().YbOrderCode))
            {
                MessageBox.Show("银豹批发开单号为空不能打印");
                return;
            }

            if (orders.FirstOrDefault().IsGroup == (int)GroupType.ShopOrder)
            {
                string whloeOrderCode = orders.FirstOrDefault().WhloeOrderCode;
                int shopId = orders.FirstOrDefault().BzShopeId;
                var lines = context.bzYbLineEntities.Where(x => x.BzWhloeOrderCode == whloeOrderCode).ToList();
                var orderIds = lines.Select(o => o.BzOrderId).ToList();
                var shop = context.bzShopEntities.FirstOrDefault(x => x.Id == shopId);
                PrintHelper.PaperPrinter();
                Thread.Sleep(3000);
                //拣货单
                ShopPackageOrder packageOrder = new ShopPackageOrder();
                packageOrder.ShopAdmin = shop.ShopAdmin;
                packageOrder.ShopName = shop.ShopName;
                packageOrder.YbOrderCode = orders.FirstOrDefault().YbOrderCode;
                packageOrder.Phone = shop.Phone;

                List<BzOrderItemEntity> orderItems = new List<BzOrderItemEntity>();
                var items = context.bzOrderItemEntities.Where(x => orderIds.Contains(x.OrderId)).ToList();
                var itemCodes = items.Select(x => x.OrderItemCode).Distinct();
                foreach (var info in itemCodes)
                {
                    int count = items.Count(x => x.OrderItemCode == info);
                    var item = items.FirstOrDefault(x => x.OrderItemCode == info);
                    item.Quantity = count;
                    orderItems.Add(item);
                }
                packageOrder.orderItems = orderItems;
                string PackagePath = PDFHelper.CreateShopOrderPackagePDF(packageOrder);
                PDFHelper.PrintPDF(PackagePath);

                ///取货对货单
                var bzorders = context.bzOrderEntities.Where(x => orderIds.Contains(x.Id)).ToList();
                List<ShopPickUpOrder> pickUpOrders = new List<ShopPickUpOrder>();
                string shopName = bzorders.FirstOrDefault().OrderShopName;
                foreach (var item in bzorders)
                {
                    ShopPickUpOrder pickUpOrder = new ShopPickUpOrder();
                    pickUpOrder.ShopName = shopName;
                    pickUpOrder.WhloeOrderCorde = whloeOrderCode;
                    pickUpOrder.order = item;
                    pickUpOrder.orderItems = context.bzOrderItemEntities.Where(x => x.OrderId == item.Id).ToList();
                    pickUpOrders.Add(pickUpOrder);
                }
                string PickUpPath = PDFHelper.CreateShopOrderPickUpPDF(pickUpOrders);
                PDFHelper.PrintPDF(PickUpPath);
                PrintHelper.FaceSheetPrinter();
                Thread.Sleep(3000);
                foreach (var item in bzorders)
                {
                    LabelHelper.PrintShopLabel(item);
                }

                MessageBox.Show("打印完成");
            }
            else
            {
                PrintHelper.PaperPrinter();
                Thread.Sleep(3000);
                string whloeOrderCode = orders.FirstOrDefault().WhloeOrderCode;
                int shopId = orders.FirstOrDefault().BzShopeId;
                var lines = context.bzYbLineEntities.Where(x => x.BzWhloeOrderCode == whloeOrderCode).ToList();
                int bzOrderId = lines.FirstOrDefault().BzOrderId;
                var bzorder = context.bzOrderEntities.FirstOrDefault(x => x.Id == bzOrderId);

                //拣货单
                TeamPackageOrder teamPackageOrder = new TeamPackageOrder();
                teamPackageOrder.AdminAddress = bzorder.Address;
                teamPackageOrder.AdminName = bzorder.MemberName;
                teamPackageOrder.Phone = bzorder.Phone;
                teamPackageOrder.YbOrderCode = orders.FirstOrDefault().YbOrderCode;
                teamPackageOrder.ShopName = bzorder.OrderShopName;
                var items = context.bzOrderItemEntities.Where(x => x.OrderId == bzOrderId);
                if (items.Count() == 1)
                {
                    teamPackageOrder.orderItems.Add(items.FirstOrDefault());
                }
                else
                {
                    var itemCodes = items.Select(x => x.OrderItemCode).Distinct();
                    foreach (var info in itemCodes)
                    {
                        int count = items.Count(x => x.OrderItemCode == info);
                        var item = items.FirstOrDefault(x => x.OrderItemCode == info);
                        item.Quantity = count;
                        teamPackageOrder.orderItems.Add(item);
                    }
                }
                string PackagePath = PDFHelper.CreateTeamOrderPackagePDF(teamPackageOrder);

                PDFHelper.PrintPDF(PackagePath);

                //取货对货单
                var orderIds = lines.Select(o => o.BzOrderId).ToList();
                var bzorders = context.bzOrderEntities.Where(x => orderIds.Contains(x.Id)).ToList();
                string bzOrderCode = bzorders.FirstOrDefault().OrderCode;
                TeamPickUpOrder teamPickUpOrder = new TeamPickUpOrder();
                teamPickUpOrder.WhloeOrderCorde = whloeOrderCode;
                teamPickUpOrder.ShopName = bzorders.FirstOrDefault().OrderShopName;
                teamPickUpOrder.Address = bzorders.FirstOrDefault().Address;
                teamPickUpOrder.AdminName = bzorders.FirstOrDefault().MemberName;
                teamPickUpOrder.Phone = bzorders.FirstOrDefault().Phone;
                teamPickUpOrder.itemEntities = context.teamOrderItemEntities.Where(x => x.BzOrderId == bzOrderCode).ToList();
                string PickUpPath = PDFHelper.CreateTeamOrderPickUpPDF(teamPickUpOrder);
                PDFHelper.PrintPDF(PickUpPath);

                PrintHelper.FaceSheetPrinter();
                Thread.Sleep(3000);
                foreach (var item in bzorders)
                {
                    LabelHelper.PrintTeamLabel(item);
                }

                MessageBox.Show("打印完成");
            }
        }
    }
}